<?php
define('THUMB_WIDTH', 300);
define('THUMB_HEIGHT', 300);
define('THUMB_QUALITY', 85);
class NewappApp extends MallbaseApp {
	//注册
	function register() {
		try {
			$db=&db();
			$a_mod =&m('authcode');
			$tel = trim ( $_REQUEST ['uname'] );
			$password = $_REQUEST ['pwd'];
			$code = $_REQUEST ['code'];
			$pai = $_REQUEST ['pai'];
			$real_name = $_REQUEST ['real_name'];

			if(empty($tel) or strlen(strval($tel))!=11){
				throw new Exception("手机号码不正确");
			}

			$a_member=&m("member");

			if(!$a_member->unique($tel)){
				throw new Exception("当前手机号码已注册");
			}

			$this->PrivateAuthcode($tel,$code,'注册');

			if(empty($password)){
				throw new Exception("密码不能为空！");
			}
			if(strlen($password)<6){
				throw new Exception("密码长度至少是6位！");
			}

			$data = array();
			$data['a_status']  =  1;
			$a_mod->edit($code["a_tid"],$data);
			$a_member=&m("member");

			$pai1='';
			$grade='';
			$sql = "SELECT d_value,d_key from zys_dictionary where d_code='pai' and d_del=1 and d_key='".$pai."'";
			$data = $db->getRow($sql);
			if($data){
				$pai1=$data['d_value'];

				$sql = "SELECT d_value from zys_dictionary where d_code='$pai' and d_del=1 and d_assist=0";
				$grade = $db->getOne($sql);
			}

			if($real_name){
				$sql="select 1 from zys_member where real_name='$real_name'";
				$u=$db->getOne($sql);
				if($u){
					throw new Exception("昵称已经被抢先使用了！");
				}
			}else{
				$real_name='嗨享'.rand(1000000, 9999999);
			}

			$data = array();
			$data['user_name']      =  $tel;
			$data['phone_tel']    =   $tel;
			$data['password']   =   md5($password);
			$data['reg_time']   =  gmtime();
			$data['real_name']   =  $real_name;
			$data['pai']   =  $pai1;
			$data['paicode']   =  $pai;
			$data['grade']   =  $grade;

			if(!$user_id = $a_member->add($data)){
				throw new Exception($a_member->get_error());
			}

			$ms = & ms ();

			/* 通过验证，执行登陆操作 */
			$this->_do_login ($user_id);

			/* 同步登陆外部系统 */
			$synlogin = $ms->user->synlogin ($user_id);

			/*同步EJABBERD_REGISTER_USER*/
			$params=array('user'=>$user_id.'','host'=>CHATURI,'password'=>CHATUSERP);
			$request = xmlrpc_encode_request('register', $params, (array('encoding' => 'utf-8')));
			try{
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, EJABBERD_REGISTER_USER);
				curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: XMLRPC::Client mod_xmlrpc","Content-Type: text/xml","Content-Length: ".strlen($request)));
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
				$output = curl_exec($ch);
				curl_close($ch);
			}
			catch(Exception $e){
				throw new Exception($e->getMessage());
			}
			$response = xmlrpc_decode($output);

			if (xmlrpc_is_fault($response)) {
				throw new Exception("xmlrpc: $response[faultString] ($response[faultCode])");
			}
			//同步Ejabbered成功

			$this->json_result ( '', '注册成功' );
		} catch ( Exception $e ) {
			$this->json_error ($e->getMessage());
			exit ();
		}
	}
}
?>